<?php

namespace app\api\controller\admin;

use app\common\controller\Api;
use think\Db;

/**
 * 软件列表接口
 */
class Software extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    public function index()
    {

    }

    /**
     * 查看
     */
    public function gindex()
    {

        $params = $this->request->param();

        $action =$params['action'];
        if($action=='apiindex'){
            $data= self::apiindex();
            return $data;
        }elseif ($action=='apiadd'){
            self::apiadd();
        }elseif ($action=='apiedit'){
            self::apiedit();
        }elseif ($action=='apidel'){
            self::apidel();
        }elseif ($action=='apiall'){
            self::apiall();
        }elseif ($action=='apiget'){
            self::apiget();
        }elseif ($action=='apiqk'){
            self::apiqk();
        }elseif ($action=='apixl'){
            self::apixl();
        }


    }

    /**
     * 查看
     */
    public function apiindex()
    {
//        halt(555);
        $params = $this->request->param();
        $pageSize =$params['pageSize'];
        $pageNum =$params['pageNum'];
        $name =isset($params['name']) ? $params['name'] : '';
        $sort=isset($params['sort']) ? $params['sort'] : 'id';
        $order=isset($params['order']) ? $params['order'] : 'desc';
        $systemFlag=isset($params['systemFlag']) ? $params['systemFlag'] : '';
        $where=[];
        if ($systemFlag){
            $where['id']  = array('like',  '%'.$systemFlag."%");
        }
        if ($name){
            $where['name']  = array('like',  '%'.$name."%");
        }
//        $pageNum=1;
//        $pageSize=1;
//        $sort='id';
//        $order='desc';
//halt($where);
        $pageNum=($pageNum-1)*$pageSize;
        $list = Db::table('fa_g_software')
            ->where($where)
//            ->field(['password', 'salt', 'token'], true)
            ->order($sort, $order)
            ->limit($pageNum,$pageSize)
            ->select();
//        halt($list);
        $total= Db::table('fa_g_software')
            ->where($where)
//            ->field(['password', 'salt', 'token'], true)
            ->order($sort, $order)
            ->limit(1,2)
            ->count();
//        foreach ($list as $k => &$v) {
////            dump($v['forcelist']);
//            $groups = isset($adminGroupName[$v['id']]) ? $adminGroupName[$v['id']] : [];
//            $v['groups'] = implode(',', array_keys($groups));
//            $v['groups_text'] = implode(',', array_values($groups));
//        }
//        unset($v);
        $result = array("total" => $total, "rows" => $list);
        return json($result);

    }
    /**
     * add
     */
    public function apiadd()
    {
        $post = $this->request->post();
        unset($post['action']);
        $post['createtime']=time();
//        halt($post);
//        ->strict(false)
        $list=Db::table('fa_g_software')->insertGetId($post);

        $this->success($list);

    }
    /**
     * up
     */

    public function apiedit()
    {
        $post = $this->request->post();
        unset($post['action']);

        $post['updatetime']=time();
        $list=Db::table('fa_g_software')->update($post);
        $this->success($list);

    }
    public function apidel()
    {
        $post = $this->request->post();
        unset($post['action']);
        $list=Db::table('fa_g_software')->delete($post);
        $this->success($list);
    }
    /**
     * all
     * //            获取所有软件列表
     */
    public function apiall()
    {
        $list=Db::table('fa_g_software')->select();

        $this->success('',$list);

    }
    /**
     * get
     * //            批量操作
     */
    public function apiget()
    {

//        逻辑待完成---看业务需求---没有定需求
        $post = $this->request->post();
        unset($post['action']);

        $list=Db::table('fa_g_software')->select();

        $this->success('',$list);

    }

    /**
     * get
     * //            批量取卡
     */
    public function apiqk()
    {

//        逻辑待完成---看业务需求---没有定需求
        $post = $this->request->post();
        unset($post['action']);

        $list=Db::table('fa_g_cardrefill')->select();

        $this->success('',$list);

    }

    /**
     *
     * //            销量查询
     */
    public function apixl()
    {

//        逻辑待完成---看业务需求---没有定需求
        $post = $this->request->post();
        unset($post['action']);
//halt($post);
//        统计每个代理的-所属软件:001ww  1天卡:[11]  3天卡:[1]  90天卡:[7]

//        获取表里 所有不同代理
        $usernamelist=Db::table('fa_g_cardrefill')
            ->where('software',$post['softwarelist_name'])
            ->group('username')->select();
//拼接每个代理的 -所属软件:001ww  1天卡:[11]  3天卡:[1]  90天卡:[7]
        $text='';
        $arr=[];

                foreach ($usernamelist as $k => &$v) {
                    $znum=Db::table('fa_g_cardrefill')
                        ->where('software',$post['softwarelist_name'])
                        ->where('username',$v['username'])
                        ->where('day',7)
                        ->count();
                    $ynum=Db::table('fa_g_cardrefill')
                        ->where('software',$post['softwarelist_name'])
                        ->where('username',$v['username'])
                        ->where('day',30)
                        ->count();
                    $jnum=Db::table('fa_g_cardrefill')
                        ->where('software',$post['softwarelist_name'])
                        ->where('username',$v['username'])
                        ->where('day',60)
                        ->count();
                    $nnum=Db::table('fa_g_cardrefill')
                        ->where('software',$post['softwarelist_name'])
                        ->where('username',$v['username'])
                        ->where('day',365)
                        ->count();

                    $text="所属软件:".$post['softwarelist_name']."  7天卡:[".$znum."]  30天卡:[".$ynum."]  90天卡:[".$jnum."]  365天卡:[".$nnum."]";
                    $arr[$k]['username']=$v['username'];
                    $arr[$k]['text']=$text;

        }


        $this->success('',$arr);

    }



}
